import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_utils/api_service/vid_service.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class MoreFreeVideoController extends GetxController with GetTickerProviderStateMixin {
  final String vId;
  final sortTitles = ['最多收藏', '最新上架', '最多观看'];
  MoreFreeVideoController(this.vId);

  bool isLoading = true;
  int page = 1;
  int sort = 0;
  late final TabController tabController = TabController(length: sortTitles.length, vsync: this);
  RefreshController? refreshController;
  List<VideoModel> dataSource = [];

  fetchPageData({bool isRefresh = true, bool showLoading = false}) async {
    if (isRefresh) {
      page = 1;
    }
    if (showLoading) {
      isLoading = true;
      update();
    }
    final res = await VidService.fetchFreeareaVideoSourceList(vId, page: page, sortType: [1, 2, 3][sort]);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.list ?? []);
    page += 1;
    update();
  }
}
